package com.weichat.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.model.Event_user;
import com.model.Propertr_user;
import com.model.User;
import com.service.Event_userService;
import com.service.Propertr_userService;
import com.weichat.tool.AccessTokentool;
import com.weichat.tool.MessageUtil;
import com.weichat.tool.Sha1Util;
import com.weichat.tool.WeiChatConfig;
import com.weichat.tool.WeiChatUtil;

@RestController
public class SportController {
	@Autowired
	AccessTokentool accessTokentool;
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	WeiChatUtil weiChatUtil;
	@Autowired
	Event_userService event_userService;
	@Autowired
	Propertr_userService propertr_userService;
	
	@RequestMapping("/Sport")
	public Map Sport(@RequestParam("serverIds") String id){
		Map m=new HashMap();
		System.out.println(id);
		
		
		
		
		return m;
	}
	
	@RequestMapping("/hcConfig")
	public Map<String,String> getJSDKconfig(@RequestParam("url") String url) throws IOException{
		Map<String,String> configParam=new HashMap<String,String>();
		Sha1Util su=new Sha1Util();
		String accesstoken=(String) accessTokentool.getNewAccessToken().get("rs");
		String Ticket=su.getjsApiTicket("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=",accesstoken);
		String NonceStr=su.getNonceStr();
		String timestamp=su.getTimeStamp();
		System.out.println("url======="+url);
		String signature=su.getSha1("jsapi_ticket="+Ticket+"&noncestr="+NonceStr+"&timestamp="+timestamp+"&url="+url);
		
		
		
		configParam.put("appId", WeiChatConfig.appid);
		configParam.put("timestamp", timestamp);
		configParam.put("nonceStr", NonceStr);
		configParam.put("signature", signature);
		
		
		
		return configParam;
	}
	
	@RequestMapping("/upload")
	public String uploadImage(@RequestParam("serverIds") String mediaId,HttpServletRequest request) throws Exception{
		String rs = null;
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
        String dateNowStr = sdf.format(now.getTimeInMillis()); 
        String openId=(String) request.getSession().getAttribute("openId");
        User u=(User) request.getSession().getAttribute("user");
        
        
		String path1=File.separator+"tmp"+File.separator+"life"+File.separator+u.getName()+File.separator;
		if(!new File(path1).exists())   {
		    new File(path1).mkdirs();
		  }
		String path2="运动健身"+dateNowStr+".jpg";
		String path=path1+path2;
		if(messageUtil.saveImageToDisk(mediaId, path)){
			Event_user eu=new Event_user();
			
			eu.setEventId(9);
			eu.setType("Y");
			eu.setUserId(u.getId());
			
			int eu_id=event_userService.selectEidByUidOrder(u.getId()).getEuId();
			
			
			Propertr_user pu=new Propertr_user();
			pu.setCount(10);
			pu.setpId(1);
			pu.setuId(u.getId());
			pu.setEuId(eu_id);
			pu.setEventName("运动健身");
			
			Propertr_user pu1=new Propertr_user();
			pu1.setCount(100);
			pu1.setpId(2);
			pu1.setuId(u.getId());
			pu1.setEuId(eu_id);
			pu1.setEventName("运动健身");
			
			Propertr_user pu2=new Propertr_user();
			pu2.setCount(100);
			pu2.setpId(3);
			pu2.setuId(u.getId());
			pu2.setEuId(eu_id);
			pu2.setEventName("运动健身");
			
			if((0<event_userService.insertSelective(eu))&&(0<propertr_userService.insertSelective(pu))&&(0<propertr_userService.insertSelective(pu1))&&(0<propertr_userService.insertSelective(pu2))){
				rs="Y";
			}
			
			
			
		}
		
		
		
		
		
		
		return rs;
	}
	@RequestMapping("/AfternoonSetPhone")
	public String AfternoonSetPhone(HttpServletRequest request) throws Exception{
		String rs = null;
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
        String dateNowStr = sdf.format(now.getTimeInMillis()); 
  
        String openId=(String) request.getSession().getAttribute("openId");
        User u=(User) request.getSession().getAttribute("user");
        
        Event_user eu=new Event_user();
		
		eu.setEventId(7);
		eu.setType("Y");
		eu.setUserId(u.getId());
		
		
		int eu_id=event_userService.selectEidByUidOrder(u.getId()).getEuId();
		
		
		Propertr_user pu1=new Propertr_user();
		pu1.setCount(100);
		pu1.setpId(2);
		pu1.setuId(u.getId());
		pu1.setEuId(eu_id);
		pu1.setEventName("下午放手机");
		
		Propertr_user pu2=new Propertr_user();
		pu2.setCount(100);
		pu2.setpId(3);
		pu2.setuId(u.getId());
		pu2.setEuId(eu_id);
		pu2.setEventName("下午放手机");
		
		
		if((0<event_userService.insertSelective(eu))&&(0<propertr_userService.insertSelective(pu1)&&(0<propertr_userService.insertSelective(pu2)))){
			rs="Y";
		}
		
		
		
		return rs;
	}

}
